/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GenerateDB;

import java.util.ArrayList;

/**
 *
 * @author escabia
 */
public class Tabla {
    private String nombre; 
    private ArrayList<Columna> columnas;
    private ArrayList<String> PK;
    private ArrayList<Externa> FK;
    
    public Tabla(String nombre){
        this.nombre = nombre;
        this.columnas = new ArrayList<>();
        this.PK = new ArrayList<>();
        this.FK = new ArrayList<>();
    }
    
    public String getNombre(){
        return nombre;
    }
    
    
    public void setNombre(String nombre){
        this.nombre = nombre;
    }
    
    public void addColumna(Columna c){
        this.columnas.add(c);
    }
    
    public void addColumnas(ArrayList<Columna> c){
        for(int i=0; i<c.size(); i++){
            this.columnas.add(c.get(i));
        }
    }
    
    public Columna lastColumna(){
        return this.columnas.get(this.columnas.size()-1);
    }
        
    public ArrayList<Columna> getColumnas(){
        return this.columnas;
    }
    
    public ArrayList<Columna> getColumnasPK(){
        ArrayList<Columna> ColumnasPK = new ArrayList<>();
        for(int j=0; j<PK.size(); j++){
            for(int i=0; i<this.columnas.size(); i++){
                if(PK.get(j) == null ? this.columnas.get(i).getAtributo() == null : PK.get(j).equals(this.columnas.get(i).getAtributo())){
                    ColumnasPK.add(new Columna(this.columnas.get(i)));
                }
            }
        }
        return ColumnasPK;
    }
    
    public ArrayList<String> getPK(){
        return PK;
    }
    
    public void setPK(ArrayList<String> PK){
        this.PK = PK;
    }
    
    public void addPK(String PK){ 
        this.PK.add(PK+"");
    }
    
    public ArrayList<Externa> getFK(){
        return this.FK;
    }
    
    public void addFK(Externa ex){
        this.FK.add(ex);
    }
    

    
    public String toCREATE(){
        /** CREATE TABLE **/
        
        String create;
        String atributos = "";
        for(int i=0; i<this.columnas.size(); i++){
            atributos += this.columnas.get(i).toSQL()+"\n";
        }
        atributos = atributos.substring(0, atributos.length()-2);
        
        create = "CREATE TABLE " + this.nombre + "\n"
                +"(\n"
                + atributos
                + "\n);";
        
        
        /** ALTER TABLE PRIMARY KEY **/
        if(this.PK.size() > 0){
            String primaria = "";
            for(int i=0; i<this.PK.size(); i++){
                primaria += this.PK.get(i) + ",";
            }
            primaria = primaria.substring(0, primaria.length()-1);


            create += "\n"
                    + "ALTER TABLE "
                    + this.nombre
                    + " \n"
                    + "ADD CONSTRAINT PK_"+this.nombre+" "
                    + "PRIMARY KEY ("
                    + primaria
                    + ");";            
        }

        
        return create;
    }
    
    public String toFK(){
        String create = "";
        /** CLAVES EXTERNAS **/
        if(this.FK.size() > 0){
            
            for(int i=0; i<this.FK.size(); i++){
                String externa = "";
                for(int j=0; j<this.FK.get(i).getPK().size(); j++){
                    externa += this.FK.get(i).getPK().get(j) + ",";
                }
                externa = externa.substring(0, externa.length()-1);
                
                String externaref = "";
                for(int j=0; j<this.FK.get(i).getPK().size(); j++){
                    externaref += this.FK.get(i).getPK().get(j) + this.FK.get(i).getPostfijo() + ",";
                }
                externaref = externaref.substring(0, externaref.length()-1);                

                create += "\n"
                        + "ALTER TABLE "
                        + this.nombre
                        + " \n"
                        + "ADD (CONSTRAINT FK_"+this.nombre+"_"+this.FK.get(i).getEntidad()+"_"+this.FK.get(i).getPostfijo()+" "
                        + "FOREIGN KEY ("
                        + externaref
                        + ") REFERENCES " + this.FK.get(i).getEntidad()
                        + " ("
                        + externa
                        + "));";            
            }
        }
        return create;
    }
} 